package com.kejiguan.ui.presenter;

import android.util.Log;

import com.android.volley.error.VolleyError;
import com.android.volley.response.Response;
import com.kejiguan.ui.event.RefreshNeStateEvent;
import com.kejiguan.ui.model.DisplayArea;
import com.kejiguan.ui.model.ExhibitionNeItem;
import com.kejiguan.ui.request.GsonRequest;
import com.kejiguan.ui.request.RequestUtil;
import com.kejiguan.ui.response.AlarmResponse;
import com.kejiguan.ui.response.CheckNeItemsStateResponse;
import com.kejiguan.ui.util.CacheDataUtil;
import com.kejiguan.ui.util.CommonDate;
import com.kejiguan.ui.util.DateFormatUtil;

import org.greenrobot.eventbus.EventBus;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by apple on 16/4/21.
 */
public class MainActivityPresenter extends BaseActivityPresenter{


    public void checkNeItemsState()
    {
        Log.e(MainActivityPresenter.class.getSimpleName(),"checkNeItemsState()方法执行了");
        long currentTime = System.currentTimeMillis();
        String currentDate = String.valueOf(currentTime);
        try {
            currentDate = DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Map<String, String> params = new HashMap<>();
        params.put("requestCode", "703");
        params.put("messageId", currentDate + "S" + currentTime);
        params.put("systemCode", CommonDate.SystemCode);

        final GsonRequest<CheckNeItemsStateResponse> request = new GsonRequest("checkNeItemsState", CheckNeItemsStateResponse.class, new Response.Listener<CheckNeItemsStateResponse>() {
            @Override
            public void onResponse(CheckNeItemsStateResponse response) {

                if ("0000".equals(response.getRetCode())&&null!=response.getExhibitionRecordMap()) {


                    response.countRunnningData();

                    Set<String> keys=response.getExhibitionRecordMap().keySet();
                    Iterator<String> stringIterator=keys.iterator();
                    while (stringIterator.hasNext())
                    {
                        String key=stringIterator.next();
                        String[] mutyKeys=key.split("=");

                        if (CacheDataUtil.getInstance().getDisPlayAreaIdNames().contains(key))
                        {

                            int disPalyAreaCount=CacheDataUtil.getInstance().getDisplayAreas().size();
                            for (int i=0;i<disPalyAreaCount;i++)
                            {
                                if (mutyKeys[0].equals(CacheDataUtil.getInstance().getDisplayAreas().get(i).getDisplayId()))
                                {
                                    Collections.sort(response.getExhibitionRecordMap().get(key));
                                    CacheDataUtil.getInstance().getDisplayAreas().get(i).setExhibitionNeItems(response.getExhibitionRecordMap().get(key));
                                }
                            }

                        }else//新增操作
                        {

                            DisplayArea displayArea=new DisplayArea();
                            displayArea.setDisplayId(mutyKeys[0]);
                            displayArea.setDisplayName(mutyKeys[1]);
                            Collections.sort(response.getExhibitionRecordMap().get(key));
                            displayArea.setExhibitionNeItems(response.getExhibitionRecordMap().get(key));

                            CacheDataUtil.getInstance().getDisplayAreas().add(displayArea);
                            CacheDataUtil.getInstance().getDisPlayAreaIdNames().add(key);
                        }
                    }

//                    List<ExhibitionNeItem> exhibitionNeItems=new ArrayList<>(0);

                    for (int i=0;i<CacheDataUtil.getInstance().getDisplayAreas().size();i++)
                    {
                        DisplayArea displayArea= CacheDataUtil.getInstance().getDisplayAreas().get(i);
                        displayArea.initDatas();

//                        List<ExhibitionNeItem> neItems=displayArea.getExhibitionNeItems();
//                        if (null!=neItems)
//                        {
//                            for (int z=0;z<neItems.size();z++)
//                            {
//                                ExhibitionNeItem neItem=neItems.get(z);
//                                if (neItem.getExhibitionState().equals(CommonDate.STATE_EXCEPTION)||
//                                        neItem.getExhibitionState().equals(CommonDate.STATE_OPEN_TIME_OUT)||
//                                        neItem.getExhibitionState().equals(CommonDate.STATE_CLOSE_TIME_OUT))
//                                {
//                                    exhibitionNeItems.add(neItem);
//                                }
//                            }
//                        }
                    }

//                    CacheDataUtil.getInstance().setAlarmExhibitionNeItems(exhibitionNeItems);

                    EventBus.getDefault().post(new RefreshNeStateEvent());
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });

        RequestUtil.sendRequest(mContext, params, request);
    }



    public void checkAlarmInfos()
    {
        long currentTime = System.currentTimeMillis();
        String currentDate = String.valueOf(currentTime);
        try {
            currentDate = DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Map<String, String> params = new HashMap<>();
        params.put("requestCode", "713");
        params.put("messageId", currentDate + "S" + currentTime);
        params.put("systemCode", CommonDate.SystemCode);
        params.put("operType","checkAlarm");


        final GsonRequest<AlarmResponse> request = new GsonRequest("checkAlarmInfos", AlarmResponse.class, new Response.Listener<AlarmResponse>() {
            @Override
            public void onResponse(AlarmResponse response) {
                if (response.getRetCode().equals("0000"))
                {
//                    response.resetExhibitionState();
                }
                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });

        RequestUtil.sendRequest(mContext, params, request);

    }

}
